package com.darly.book.presenter;

import com.darly.book.model.bean.BookListBean;
import com.darly.book.model.flag.BookListType;
import com.darly.book.model.remote.BookApi;
import com.darly.book.presenter.contract.BookListContract;
import com.darly.book.ui.base.RxPresenter;
import com.darly.book.utils.Constant;
import com.darly.dlcommon.common.dlog.DLog;
import com.darly.dlcommon.retrofit.RxjavaRetrofitRequestUtil;

import java.util.List;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BookListPresenter extends RxPresenter<BookListContract.View> implements BookListContract.Presenter {


    @Override
    public void refreshBookList(BookListType type, String tag, int start, int limited) {

        if (tag.equals("全本")) {
            tag = "";
        }

        Disposable refreshDispo = getBookListSingle(type, tag, start, limited)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (beans) -> {
                            mView.finishRefresh(beans);
                            mView.complete();
                        }
                        ,
                        (e) -> {
                            mView.complete();
                            mView.showError();
                            DLog.e(e);
                        }
                );
        addDisposable(refreshDispo);
    }

    @Override
    public void loadBookList(BookListType type, String tag, int start, int limited) {
        Disposable refreshDispo = getBookListSingle(type, tag, start, limited)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        (beans) -> {
                            mView.finishLoading(beans);
                        }
                        ,
                        (e) -> {
                            mView.showLoadError();
                            DLog.e(e);
                        }
                );
        addDisposable(refreshDispo);
    }

    private Single<List<BookListBean>> getBookListSingle(BookListType type, String tag, int start, int limited) {
        Single<List<BookListBean>> bookListSingle = null;
        //数据类型转换
        String gender = "";
        if (tag.equals("男生")) {
            gender = "male";
            tag = "";
        } else if (tag.equals("女生")) {
            gender = "female";
            tag = "";
        }

        switch (type) {
            case HOT:
                bookListSingle = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                        .getBookListPackage(type.getNetName(), "collectorCount", start + "", limited + "", tag, gender)
                        .map(bean -> bean.getBookLists());

                break;
            case NEWEST:
            case COLLECT:
                bookListSingle = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL)
                        .getBookListPackage("all", type.getNetName(), start + "", limited + "", tag, gender)
                        .map(bean -> bean.getBookLists());
                break;
        }
        return bookListSingle;
    }
}
